import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;


//ClientChat class.
public class ClientChat 
{
	
	JFrame frame2;
	JPanel panel2;
	JTextField text2;
	JTextArea area2;
	Socket clientsocket;
	ObjectOutputStream output;
	ObjectInputStream input;
	JScrollPane scrollpane;
	
	//Constructor for the ClientChat.
	public ClientChat() throws IOException 
	{
		frame();
	}
	
	//Designed GUI in this frame method.
	void frame() throws IOException
	{
		frame2=new JFrame("Client Chat");
		frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		panel2=new JPanel();
		panel2.setLayout(null);
		text2=new JTextField();
		
		text2.setFocusable(true);
		text2.setBounds(10, 220, 260, 50);
		text2.addActionListener(new ActionListener() 
		{
			
			public void actionPerformed(ActionEvent e)
			{
				try {
					sendMessage(e.getActionCommand());
				} catch (IOException e1) {
					e1.printStackTrace();
				}
				text2.setText("");
				
			}
		});
		panel2.add(text2);
		scrollpane=new JScrollPane();
		scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
		scrollpane.setBounds(290, 100, 20, 50);
		area2=new JTextArea();
		area2.setFocusable(false);
		area2.setBounds(10, 10, 260, 200);
		panel2.add(area2);
		panel2.setBackground(Color.DARK_GRAY);
		frame2.add(panel2);
		frame2.setLocation(500,200);
		frame2.pack();
		frame2.setSize(300, 320);
		frame2.setVisible(true);

	}
	
	//When ever Client executes it invokes the startRunning method.
	public void startRunning() throws UnknownHostException, IOException, ClassNotFoundException
	{
		
		while(true)
		{
			connectStarted();
			messageStreams();
			whileChatting();
			closeConnection();
		}
	}
	
	//Intializing Socket to request server and server running on a particular port.
	private void connectStarted() throws UnknownHostException, IOException
	{
		clientsocket=new Socket("localhost", 8070);
						
	}
	
	//To pass input and output stream messages.
	private void messageStreams() throws IOException
	{
		output=new ObjectOutputStream(clientsocket.getOutputStream());
		output.flush();
		input=new ObjectInputStream(clientsocket.getInputStream());
	}
	
	//To read messages from the server.
	private void whileChatting() throws ClassNotFoundException, IOException
	{
		String message="";
		sendMessage(message);
		ableToType(true);
		do
		{
			message=(String)input.readObject();
			showMessage("\n"+message);
		}while(!message.equals("SERVER - END"));
	}
	
	//Closing Client connection.
	private void closeConnection() throws IOException
	{
		ableToType(false);
		output.close();
		input.close();
	}
	
	//To send messages from client to server.
	private void sendMessage(String message) throws IOException
	{
		output.writeObject(message);
		output.flush();
		showMessage("\n"+message);
	}
	
	//To display messages on textarea.
	private void showMessage(final String text)
	{
		SwingUtilities.invokeLater(new Runnable() 
		{
			public void run() 
			{
				area2.append(text);
				
			}
		});
	}
	
	//To type message in text box, whenever the connection terminated we were not able to type messages in textbox.
	private void ableToType(final boolean tof)
	{
		SwingUtilities.invokeLater(new Runnable() 
		{
			public void run() 
			{
				text2.setEditable(tof);
			}
		});
	}
}
